//
//  CNMainViewController.swift
//  Ccweibo
//
//  Created by 陈锦聪 on 16/3/19.
//  Copyright © 2016年 Coco. All rights reserved.
//

import UIKit
import SVProgressHUD

class CNMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let mainTabBar = CNMainTabBar()
        
        mainTabBar.composeClosure = {
            if !CNUserAccountViewModel.sharedInstance.isLogin {
                SVProgressHUD.showErrorWithStatus("请先登录")
                return
            }
//            print("点击了撰写微博按钮")
            let composeVC = CNComposeViewController()
            
            self.presentViewController(UINavigationController(rootViewController: composeVC), animated: true, completion: nil)
        }
        
        setValue(mainTabBar, forKey: "tabBar")
        
        addChildViewControllers()
    }
    
    private func addChildViewControllers() {
        
        let homeVC = CNHomeViewController()
        addChildViewController(homeVC, title: "首页", imageName: "tabbar_home")
        
        let messageVC = CNMessageViewController()
        addChildViewController(messageVC, title: "消息", imageName: "tabbar_message_center")
        
        let discoverVC = CNDiscoverViewController()
        addChildViewController(discoverVC, title: "发现", imageName: "tabbar_discover")
        
        let profileVC = CNProfileViewController()
        addChildViewController(profileVC, title: "我", imageName: "tabbar_profile")
    }
    
    
    private func addChildViewController(controller: UIViewController, title: String, imageName: String) {
        controller.title = title
        // tintColor:统一设置字体和图片的渲染颜色
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: UIControlState.Selected)
        
        controller.tabBarItem.image = UIImage(named: imageName)
        
        let highlightedImageName = imageName + "_highlighted"
        // 渲染模式:AlwaysOriginal
        controller.tabBarItem.selectedImage = UIImage(named: highlightedImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        addChildViewController(UINavigationController(rootViewController: controller))
    }
}
